<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
    <el-form-item label="客户" prop="customerId">
      <el-select filterable v-model="dataForm.customerId" placeholder="请选择">
        <el-option
          v-for="item in optionsCustomer"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="租仓" prop="storageId">
      <el-select filterable v-model="dataForm.storageId" placeholder="请选择">
        <el-option
          v-for="item in storageOptions"
          :key="item.id"
          :label="item.storageName"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="支付方式" prop="payWay">
      <el-select v-model="dataForm.payWay" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="是否需要车辆" prop="needCar">
      <template>
        <el-radio v-model="dataForm.needCar" :label="1">需要</el-radio>
        <el-radio v-model="dataForm.needCar" :label="2">不需要</el-radio>
      </template>
    </el-form-item>
    <el-form-item label="租赁开始时间 " prop="rentStartTime">
      <div class="block">
        <!-- <span class="demonstration">带快捷选项</span> -->
        <el-date-picker
          v-model="dataForm.rentStartTime"
          align="right"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="租赁结束时间 " prop="rentEndTime">
      <div class="block">
        <!-- <span class="demonstration">带快捷选项</span> -->
        <el-date-picker
          v-model="dataForm.rentEndTime"
          align="right"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="面积" prop="spaceSize">
      <el-input type="number" v-model="dataForm.spaceSize" placeholder="面积" :step="0.1" min="0"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input type="number" v-model="dataForm.price" placeholder="价格" :step="0.1" min="0.1"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  export default {
    data () {
      return {
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }]
        },
        value: '',
        optionsCustomer: [],
        storageOptions: [],
        options: [],
        visible: false,
        dataForm: {
          id: 0,
          stoContractNum: '',
          customerId: '',
          storageId: '',
          spaceSize: '',
          payWay: '',
          price: '',
          needCar: '',
          rentStartTime: '',
          rentEndTime: '',
          delFlag: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: ''
        },
        dataRule: {
          stoContractNum: [
            { required: true, message: '合同编号不能为空', trigger: 'blur' }
          ],
          customerId: [
            { required: true, message: '客户id不能为空', trigger: 'blur' }
          ],
          storageId: [
            { required: true, message: '仓库id不能为空', trigger: 'blur' }
          ],
          spaceSize: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          payWay: [
            { required: true, message: '支付方式不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          needCar: [
            { required: true, message: '是否需要车辆不能为空', trigger: 'blur' }
          ],
          rentStartTime: [
            { required: true, message: '租赁开始时间 不能为空', trigger: 'blur' }
          ],
          rentEndTime: [
            { required: true, message: '租赁结束时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
      created() {
      this.getStorageList(),
      this.getcustomerList(),
      this.getList()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entstoragecontract/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.stoContractNum = data.entStorageContract.stoContractNum
                this.dataForm.customerId = data.entStorageContract.customerId
                this.dataForm.storageId = data.entStorageContract.storageId
                this.dataForm.spaceSize = data.entStorageContract.spaceSize
                this.dataForm.payWay = data.entStorageContract.payWay
                this.dataForm.price = data.entStorageContract.price
                this.dataForm.needCar = parseInt(data.entStorageContract.needCar)
                this.dataForm.rentStartTime = data.entStorageContract.rentStartTime
                this.dataForm.rentEndTime = data.entStorageContract.rentEndTime
              }
            })
          }
        })
      },
       getcustomerList(){
        // 客户接口
        this.$http({
          url: this.$http.adornUrl(`/app/entcustomer/listCustomer`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.optionsCustomer = data.list
          } else {
            this.optionsCustomer = []
          }
        })
      },
      getStorageList(){
        this.$http({
          url: this.$http.adornUrl(`/app/entcoldstorage/storageName/list`),
          method: "get",
          params: this.$http.adornParams({
            ...this.dataForm
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            console.log("xxxxxxx",data)
            this.storageOptions = data.coldStorageList
          }
        })
      },
      getList(){
        this.$http({
          url: this.$http.adornUrl(`/app/dictitem/getListById/pay_way`),
          method: "get",
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.options = data.list
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entstoragecontract/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'stoContractNum': this.dataForm.stoContractNum,
                'customerId': this.dataForm.customerId,
                'storageId': this.dataForm.storageId,
                'spaceSize': this.dataForm.spaceSize,
                'payWay': this.dataForm.payWay,
                'price': this.dataForm.price,
                'needCar': this.dataForm.needCar,
                'rentStartTime': this.dataForm.rentStartTime,
                'rentEndTime': this.dataForm.rentEndTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
